<?php
/**
 * Anything added to this helper will be available to all templates in the Files controller.
 */
 
function create_breadcrumb($path,$location='index')
{
    foreach( $path as $id=>$name )
    {
       echo '/ '.link_to($name,array('controller'=>'admin/files' ,'action'=>"$location?file_folder_id=$id"));
    }
}
 
function path_to_file($folder_id)
{
    $folders = new File_Folders();
    $path = $folders->get_public_path($folder_id);
    return get_installed_path().'/'.$path;
}

function public_path_to_file($file_id)
{
    use_model('files','file_folders');
    $files = new Files();
    $file_folders = new File_Folders();
    $file = $files->find(
                        array(
                            'search'=>'first',
                            'conditions'=> array('id=?',$file_id),
                            'fields' => array('file_folder_id','name')
                            ));
    return path_to_file($file['file_folder_id']).'/'.$file['name'];
}

function get_icon($mime)
{
    $types = new File_Types();
    $type  = $types->find( array('search'=>'first','conditions'=>array('mime=?',$mime))); 
    if ( !empty($type['icon']) )
    {
        $icon =  '/images/admin/icons/'.$type['icon'];
    } else {
        $icon = '/images/admin/icons/file.png';
    }
    
    return "<img src='".get_installed_path()."$icon' align='absmiddle' />";
}

function get_allowed_types()
{
    $file_types = new File_Types();
    $types = $file_types->get_allowed_types();
    // make a comma delimited list
    $allowed = array();
    foreach( $types as $type)
    {
        $allowed[] = $type['ext'];
    }
    return implode(',',$allowed);
}

function get_file_icons()
{
     $sourcedir = opendir(PUBLIC_HTML.'/images/admin/icons'); 
     $icons = array();
        // delete all file children  
        while(false !== ($filename = readdir($sourcedir)))
        {           
            if(!preg_match("#^\.#",$filename) && $filename != 'foldericon.png'){
                $icons[$filename] = $filename;
            }
        }
        closedir($sourcedir);  
        return $icons;      
}
?>